package cn.iocoder.yudao.module.product.controller.admin.productmanagement;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.product.controller.admin.productmanagement.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.productmanagement.ProductManagementDO;
import cn.iocoder.yudao.module.product.convert.productmanagement.ProductManagementConvert;
import cn.iocoder.yudao.module.product.service.productmanagement.ProductManagementService;

@Tag(name =  "管理后台 - 产品")
@RestController
@RequestMapping("/product/management")
@Validated
public class ProductManagementController {

    @Resource
    private ProductManagementService managementService;

    @PostMapping("/create")
    @Operation(summary = "创建产品")
    @PreAuthorize("@ss.hasPermission('product:management:create')")
    public CommonResult<Long> createManagement(@Valid @RequestBody ProductManagementCreateReqVO createReqVO) {
        return success(managementService.createManagement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品")
    @PreAuthorize("@ss.hasPermission('product:management:update')")
    public CommonResult<Boolean> updateManagement(@Valid @RequestBody ProductManagementUpdateReqVO updateReqVO) {
        managementService.updateManagement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:management:delete')")
    public CommonResult<Boolean> deleteManagement(@RequestParam("id") Long id) {
        managementService.deleteManagement(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:management:query')")
    public CommonResult<ProductManagementRespVO> getManagement(@RequestParam("id") Long id) {
        ProductManagementDO management = managementService.getManagement(id);
        return success(ProductManagementConvert.INSTANCE.convert(management));
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('product:management:query')")
    public CommonResult<List<ProductManagementRespVO>> getManagementList(@RequestParam("ids") Collection<Long> ids) {
        List<ProductManagementDO> list = managementService.getManagementList(ids);
        return success(ProductManagementConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品分页")
    @PreAuthorize("@ss.hasPermission('product:management:query')")
    public CommonResult<PageResult<ProductManagementRespVO>> getManagementPage(@Valid ProductManagementPageReqVO pageVO) {
        PageResult<ProductManagementDO> pageResult = managementService.getManagementPage(pageVO);
        return success(ProductManagementConvert.INSTANCE.convertPage(pageResult));
    }

}
